package com.ebt.m.customer.net.http;

import android.text.TextUtils;

import com.ebt.m.customer.entity.Attachment;
import com.ebt.m.customer.entity.PolicyBean;
import com.ebt.m.commons.utils.Logger;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.NumberFormat;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by archer.qi on 2016/12/15.
 */
public class PolicyRequest {

    public static final String Format = "application/json";
    public static final String Encoding = "utf-8";

    public static String getMediaType() {
        return Format + ";" + Encoding;
    }

    public static RequestBody getRequestBody(String json) {
        return RequestBody.create(MediaType.parse(getMediaType()), json);
    }

    private static String getField(String field) {
        return field == null ? "" : field;
    }

    private static void put(JSONObject object, String key, String value) {
        if (object != null) {
            try {
                object.put(key, getField(value));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    private static JSONArray getAttachArray(List<Attachment> attachments) {
        JSONArray arr = new JSONArray();
        if (attachments == null || (attachments != null && attachments.size() == 0)) {
            return arr;
        }
        for (Attachment attach : attachments) {
            if (attach != null) {
                if (!TextUtils.isEmpty(attach.getUrl())) {
                    JSONObject obj = new JSONObject();
                    put(obj, "attachmentName", attach.getName());
                    put(obj, "attachmentPath", attach.getUrl());
                    arr.put(obj);
                }
            }
        }
        return arr;
    }

    /**
     * Body - CustomerPolicyAttachment_Create
     *
     * @param attachments
     * @return
     */
    public static RequestBody getCreateAttachmentBody(List<Attachment> attachments) {
        if (attachments == null) {
            return null;
        }
        try {
            JSONObject jsonObj = new JSONObject();
            JSONArray arr = getAttachArray(attachments);
            if (arr != null) {
                jsonObj.put("listPolicyAttachment", arr);
            }
            return getRequestBody(jsonObj.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Body -  CustomerPolicyAttachment_Delete
     *
     * @param attachments
     * @return
     */
    public static RequestBody getDeleteAttachmentBody(List<Attachment> attachments) {
        if (attachments == null) {
            return null;
        }
        try {
            JSONObject jsonObj = new JSONObject();
            JSONArray arr = new JSONArray();
            for (Attachment attachment : attachments) {
                if (attachment != null) {
                    if (!TextUtils.isEmpty(attachment.getId())) {
                        JSONObject obj = new JSONObject();
                        put(obj, "id", attachment.getId());
                        arr.put(obj);
                    }
                }
            }
            jsonObj.put("listPolicyAttachment", arr);
            return getRequestBody(jsonObj.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Body - CustomerPolicy_Create
     *
     * @param policy
     * @return
     */
    public static RequestBody getCreatePolicyBody(PolicyBean policy) {
        if (policy == null) {
            return null;
        }
        JSONObject jsonObj = new JSONObject();
        put(jsonObj, "customerUUId", policy.getCustomerId());
        if (policy.getCompany() != null) {
            put(jsonObj, "brandId", policy.getCompany().getId());
            put(jsonObj, "brandName", policy.getCompany().getName());
        }
        if (policy.getRisk() != null) {
            put(jsonObj, "insuranceCode", policy.getRisk().getId());
            put(jsonObj, "insuranceName", policy.getRisk().getName());
        }
        put(jsonObj, "effectDateTime", policy.getEffectDate());
//        put(jsonObj, "premium", policy.getPermium() + "");
        put(jsonObj, "premium", NumberFormat.getInstance().format(policy.getPermium()).replace(",", ""));
        put(jsonObj, "applicationNo", policy.getInsureNo());
        put(jsonObj, "policyNumber", policy.getPolicyNo());
        put(jsonObj, "payPeriod", policy.getPayPeriodCode() + "");

        try {
            JSONArray arr = getAttachArray(policy.getAttachments());
            if (arr != null) {
                jsonObj.put("listPolicyAttachment", arr);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        Logger.i("->创建保单参数： " + jsonObj.toString());

        return getRequestBody(jsonObj.toString());
    }

    /**
     * Body - CustomerPolicy_Update
     *
     * @param policy
     * @return
     */
    public static RequestBody getUpdatePolicyBody(PolicyBean policy) {
        if (policy == null) {
            return null;
        }
        JSONObject jsonObj = new JSONObject();
        put(jsonObj, "uuid", policy.getPolicyId());
        put(jsonObj, "customerUUId", policy.getCustomerId());
        if (policy.getCompany() != null) {
            put(jsonObj, "brandId", policy.getCompany().getId());
            put(jsonObj, "brandName", policy.getCompany().getName());
        }
        if (policy.getRisk() != null) {
            put(jsonObj, "insuranceCode", policy.getRisk().getId());
            put(jsonObj, "insuranceName", policy.getRisk().getName());
        }
        put(jsonObj, "effectDateTime", policy.getEffectDate());
        put(jsonObj, "premium", policy.getPermium() + "");
        put(jsonObj, "applicationNo", policy.getInsureNo());
        put(jsonObj, "policyNumber", policy.getPolicyNo());
        put(jsonObj, "payPeriod", policy.getPayPeriodCode() + "");

        return getRequestBody(jsonObj.toString());
    }

}
